﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace Zentrum.Windows.Models
{
	public abstract class ViewModel : INotifyPropertyChanged
    {
		private string m_Title;

		public event PropertyChangedEventHandler PropertyChanged;

		protected ViewModel() : this(null)
		{

		}

		protected ViewModel(string title)
		{
			m_Title=title;
		}

		protected void SetAndNotify<T>(ref T target, T value, [CallerMemberName]string property="")
		{
			target=value;
			OnPropertyChanged(property);
		}

		protected void CompareSetAndNotify<T>(ref T target, T value, [CallerMemberName]string property="") where T:IEquatable<T>
		{
			if(target==null || target.Equals(value)==false)
			{
				target=value;
				OnPropertyChanged(property);
			}
		}

		public string Title
		{
			get{return m_Title;}
			set{if(m_Title!=value) SetAndNotify(ref m_Title,value);}
		}

		/// <summary>
		/// Raises the PropertyChanged event
		/// </summary>
		/// <param name="propertyName">The name of the property that has changed</param>
		protected virtual void OnPropertyChanged(string propertyName)
		{
			var d=PropertyChanged;
			if(d!=null) d(this,new PropertyChangedEventArgs(propertyName));
		}
	}
}
